package com.amumu.netctoss.dao.impl;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.amumu.netctoss.dao.AdminDao;
import com.amumu.netctoss.entity.Admin;
import com.amumu.netctoss.entity.Privilege;
import com.amumu.netctoss.util.PrivilegeReader;
@Repository
@Scope("prototype")
@Transactional  
public class HibernateAdminDao extends HibernateBaseDao implements AdminDao{
	private static final String FIND_BY_CONDITIONS="select distinct a.* from admin_info a,role r,admin_role ar,role_privilege rp where a.id=ar.admin_id and ar.role_id=r.id and rp.role_id=r.id ";
	public void delete(int id) throws Exception {
		Admin a = findById(id);
		getHibernateTemplate().delete(a);
	}

	public List<Admin> findAll(int page, int size) throws Exception {
		return findByPage(page, size, "from Admin", null);
	}

	public Admin findByCodeAndPwd(String code, String pwd) throws Exception {
		String hql = "from Admin where code=? and password=?";
		return (Admin) uniqueResult(hql, code,pwd);
	}

	public Admin findById(int id) throws Exception {
		return (Admin) getHibernateTemplate().load(Admin.class, id);
	}

	public Map<String, String> getModuleOptions() {
		Map<String, String> map = new LinkedHashMap<String, String>();
		List<Privilege> ps = PrivilegeReader.getPrivileges();
		map.put("", "全部");
		for(Privilege p:ps){
			map.put(p.getId(), p.getName());
		}
		return map;
	}

	public int getPageCount(int size) throws Exception {
		return getTotalPage(size, "select count(*) from Admin", null);
	}

	public int getPageCount(int size, String module, String roleName)
			throws Exception {
		List params = new ArrayList();
		StringBuilder sql = new StringBuilder("select count(distinct a) from Admin a join a.roles r join r.pid p where 1=1");
		if(!"".equals(module)&&module!=null){
			sql.append(" and p=?");
			params.add(Integer.parseInt(module));
		}
		if(!"".equals(roleName)&&roleName!=null){
			sql.append(" and r.name like ?");
			params.add("%"+roleName+"%");
		}
		if(params.size()==0){
			return getPageCount(size);
		}
		return getTotalPage(size, sql.toString(), params.toArray());
	}

	@Transactional
	public void modifyPwd(Admin admin) throws Exception {
		Admin a = findById(admin.getId());
		a.setPassword(admin.getPassword());
		getHibernateTemplate().update(a);
	}

	@Transactional
	public void resetPwd(Integer[] id) throws Exception {
		for(int i : id){
			Admin a = findById(i);
			a.setPassword("1234");
		}
		
	}

	public void save(Admin admin) throws Exception {
		getHibernateTemplate().save(admin);
		
	}

	public List<Admin> search(int page, int size, String module, String roleName)
			throws Exception {
		
		List params = new ArrayList();
		StringBuilder sql = new StringBuilder("select distinct a from Admin a join a.roles r join r.pid p where 1=1");
		if(!"".equals(module)&&module!=null){
			sql.append(" and p=?");
			params.add(Integer.parseInt(module));
		}
		if(!"".equals(roleName)&&roleName!=null){
			sql.append(" and r.name like ?");
			params.add("%"+roleName+"%");
		}
		if(params.size()==0){
			return findAll(page, size);
		}
		return findByPage(page, size, sql.toString(), params.toArray());
	}

	public void update(Admin admin) throws Exception {
		Admin a = findById(admin.getId());
		a.setName(admin.getName());
		a.setEmail(admin.getEmail());
		a.setTelephone(admin.getTelephone());
		a.setRid(admin.getRid());
		
	}

	@Transactional
	public void updateUserInfo(Admin admin) throws Exception {
		Admin a = findById(admin.getId());
		a.setName(admin.getName());
		a.setEmail(admin.getEmail());
		a.setTelephone(admin.getTelephone());
		
	}

}
